package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.settlement.system.common.constant.SystemConstants;
import com.settlement.system.common.enums.ConditionEnum;
import com.settlement.system.model.entity.SysColumnMatchCondition;
import com.settlement.system.mapper.SysColumnMatchConditionMapper;
import com.settlement.system.model.form.ColumnMatchConditionForm;
import com.settlement.system.service.SysColumnMatchConditionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 字段对应条件 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-09-19
 */
@Service
@RequiredArgsConstructor
public class SysColumnMatchConditionServiceImpl extends ServiceImpl<SysColumnMatchConditionMapper, SysColumnMatchCondition> implements SysColumnMatchConditionService {

    private final RedisTemplate redisTemplate;

    @Override
    @SuppressWarnings("unchecked")
    public boolean saveForm(ColumnMatchConditionForm conditionForm) {
        SysColumnMatchCondition condition = new SysColumnMatchCondition();
        BeanUtil.copyProperties(conditionForm, condition);
        // 删除缓存
        redisTemplate.delete(SystemConstants.BILL_COLUMN_CONDITION_PREFIX.formatted(condition.getMatchId()));
        return this.save(condition);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<SysColumnMatchCondition> getConditionList(String matchId) {
        // 先从缓存提取
        String key = SystemConstants.BILL_COLUMN_CONDITION_PREFIX.formatted(matchId);
        Object value = redisTemplate.opsForValue().get(key);
        if(value != null){
            if (((List<SysColumnMatchCondition>) value).size() > 0){
                return (List<SysColumnMatchCondition>) value;
            }
        }
        List<SysColumnMatchCondition> list = this.list(new LambdaQueryWrapper<SysColumnMatchCondition>()
                .eq(SysColumnMatchCondition::getMatchId, matchId)
                .orderByDesc(SysColumnMatchCondition::getUpdateTime));
        // 加入缓存
        redisTemplate.opsForValue().set(key, list, 1, TimeUnit.HOURS);
        return list;
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean deleteCondition(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysColumnMatchCondition> list = this.list(new LambdaQueryWrapper<SysColumnMatchCondition>()
                .in(SysColumnMatchCondition::getId, ids));
        // 删除缓存
        for (SysColumnMatchCondition condition : list) {
            redisTemplate.delete(SystemConstants.BILL_COLUMN_CONDITION_PREFIX.formatted(condition.getMatchId()));
        }
        return this.removeByIds(ids);
    }
}
